package com.example.status;

import com.example.model.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: ValueStatusDemo
 * Package: com.example.status
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-26
 * Time: 15:11
 */

//需求 检测每种传感器的水位值 如果连续两个水位值超过10 就输出报价

public class ValueStatusDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> data = env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));

                    }
                })
                //分配时间戳 和 设置水位线
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //时间线为升序
                                .<WaterSensor>forMonotonousTimestamps()
                                //从数据中获取时间戳
                                .withTimestampAssigner((val, ts) -> val.getTs() * 1000L)
                );

        //状态值 通过keyBy之后操作
        //检测 每种 说明要KeyBy区分
        data.keyBy(value -> value.getId())
                //需求 检测每种传感器的水位值 如果连续两个水位值超过10 就输出报价
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {

                    //定义值状态 初始化操作在open方法中 从运行时状态获取
                    ValueState<Integer> lastVc;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        //值状态描述符 参数指定 状态名字 和 状态存储的数据类型
                        lastVc = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("lastVc", Types.INT));
                    }

                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                        //1.获取上一次的水位值 第一次的水位值是null 会空指定一样
                        int last = lastVc.value() == null ? 0 : lastVc.value();
                        //2.和现在来的水位值比较
                        if (Math.abs(value.getVc() - last) > 10) {
                            //报警 输出
                            out.collect("传感器ID=" + value.getId() + ",上一次的值=" + last + " 本次水位值=" + value.getVc() + "大于10");
                        }
                        //3.更新状态
                        lastVc.update(value.getVc());

                    }
                }).print();


        env.execute();
    }

}
